var hastrue	= true;
$(document).ready(function() 
{
	var url = window.URL || window.webkitURL;
	$("#txtimage").change(function(e) {
	    if( this.disabled ){
	        alert('Your browser does not support File upload.');
	    }else{
	    	$("#error_image").css('display', 'none');
	        var chosen = this.files[0];
	        var image = new Image();
	        image.onload = function() {
	        	if (chosen.type != 'image/gift' && chosen.type != 'image/png' && chosen.type != 'image/jpeg' && chosen.type != 'image/pjpeg')
	        	{
	        		$("#error_image").css('display', 'block');
		        	$("#txtimage").attr('tabindex', -1).focus();
		        	$("#view_image").html("");
		        	$("#view_image").css('display', 'none');
	        		hastrue	= false;
	        		return false;
	        	}
	        	else if (chosen.size >  1024*1024)
	        	{
	        		$("#error_image").css('display', 'block');
	        		$("#txtimage").attr('tabindex', -1).focus();
	        		$("#view_image").html("");
	        		$("#view_image").css('display', 'none');
	        		hastrue	= false;
	        		return false;
	        	}
	        	else
	        	{
	        		$("#error_image").css('display', 'none');
	        		$("#view_image").html(image);
	        		$("#view_image").css('display', 'block');
	        		$("#view_image").find('img').css('width', '50px');
	        		hastrue	= true;
	            	//alert('Width:'+this.width +' Height:'+ this.height+' '+ Math.round(chosen.size/1024/1024)+'MB');
	        	}
	        };
	        image.onerror = function() {
	        	$("#error_image").css('display', 'block');
	        	hastrue	= false;
	        	//alert('Not a valid file type: '+ chosen.type);
	        };
	        image.src = url.createObjectURL(chosen);                    
	     }
	});
});

function GetSubCat()
{
	var cat_id		= $("#select_cat1").val();
	var base_url	= $('#base_url').val();
	$("#select_subsubcat").attr('disabled', 'disabled');
	if (cat_id != "")
	{
		$("#select_subcat").removeAttr('disabled');
		$("#select_price").removeAttr('disabled');
		$.ajax({
			type: "POST",
			url: base_url + 'getsubcat.html',
			data: {cat_id: cat_id},
			dataType: "text",
			beforeSend: function(xhr) 
			{
				xhr.setRequestHeader("Ajax-Request", "true");
			},
			success: function(response) 
			{
				$("#select_subcat").html(response);
			}
		});
	}
	else
	{
		$("#select_subcat").attr('disabled', 'disabled');
		$("#select_price").attr('disabled', 'disabled');
		$("#select_subcat").html('<option value="">==== Chọn nhu cầu ==========</option>');
		$("#select_subsubcat").html('<option value="">==== Chọn chủng loại ========</option>');
		$("#select_price").html('<option value="">==== Chọn giá chuyên mục ===</option>');
	}
}

function GetSubSubCat()
{
	var subcat_id	= $("#select_subcat").val();
	var base_url	= $('#base_url').val();
	if (subcat_id != "")
	{
		$("#select_subsubcat").removeAttr('disabled');
		$.ajax({
			type: "POST",
			url: base_url + 'getsubsubcat.html',
			data: {subcat_id: subcat_id},
			dataType: "text",
			beforeSend: function(xhr) 
			{
				xhr.setRequestHeader("Ajax-Request", "true");
			},
			success: function(response) 
			{
				$("#select_subsubcat").html(response);
			}
		});
	}
	else
	{
		$("#select_subsubcat").attr('disabled', 'disabled');
		$("#select_subsubcat").html('<option value="">==== Chọn chủng loại ========</option>');
	}
}

function GetDistinct()
{
	var city_id		= $("#select_city").val();
	var base_url	= $('#base_url').val();
	if (city_id != "")
	{
		$("#select_distinct").removeAttr('disabled');
		$.ajax({
			type: "POST",
			url: base_url + 'getdistinct.html',
			data: {city_id: city_id},
			dataType: "text",
			beforeSend: function(xhr) 
			{
				xhr.setRequestHeader("Ajax-Request", "true");
			},
			success: function(response) 
			{
				$("#select_distinct").html(response);
			}
		});
	}
	else
	{
		$("#select_distinct").attr('disabled', 'disabled');
		$("#select_distinct").html('<option value="">==== Chọn Quận/huyện ======</option>');
	}
}

$(document).ready(function() 
{
	$('#chkaccept').click(function()
	{
		var base_url	= $('#base_url').val();
		var chkaccept = $(this).attr("checked");
        if (chkaccept == true)
        {
        	$("#cmdpost").removeAttr('disabled');
        	$("#cmdpost").css('background', "url('" + base_url + "images/sites/button_dangtin3.png') top left no-repeat");
        }
        else
        {
        	$("#cmdpost").attr('disabled', 'disabled');
        	$("#cmdpost").css('background', "url('" + base_url + "images/sites/button_dangtin.png') top left no-repeat");
        }
	});
});

function checkfrmpost()
{
	var chkaccept 	= $("#chkaccept").attr("checked");
	if (chkaccept	== false)
	{
		return false;
	}
	var txttitle	= $("#txttitle").val();
	if (txttitle	== "")
	{
		$("#error_title").css('display', 'block');
		$("#txttitle").focus();
		return false;
	}
	else
		$("#error_title").css('display', 'none');
	
	var txtphone	= $("#txtphone").val();
	if (txtphone	== "")
	{
		$("#error_phone").css('display', 'block');
		$("#txtphone").focus();
		return false;
	}
	else
		$("#error_phone").css('display', 'none');
	
	var select_cat1	= $("#select_cat1").val();
	if (select_cat1	== "")
	{
		$("#error_cat").css('display', 'block');
		$("#select_cat1").focus();
		return false;
	}
	else
		$("#error_cat").css('display', 'none');
	
	var select_subcat	= $("#select_subcat").val();
	if (select_subcat	== "")
	{
		$("#error_subcat").css('display', 'block');
		$("#select_subcat").focus();
		return false;
	}
	else
		$("#error_subcat").css('display', 'none');
	
	var select_city	= $("#select_city").val();
	if (select_city	== "")
	{
		$("#error_city").css('display', 'block');
		$("#select_city").focus();
		return false;
	}
	else
		$("#error_city").css('display', 'none');
	
	var txtcontent = tinyMCE.get('txtcontent').getContent();
	if (txtcontent	== "")
	{
		$("#error_content").css('display', 'block');
		tinyMCE.get('txtcontent').focus();
		return false;
	}
	else
		$("#error_content").css('display', 'none');
}